package storeSorting

import (
	"context"
	"github.com/google/uuid"
	"insighttracking/common/utils"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/purchase/internal/svc"
	"insighttracking/apps/purchase/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetStoreSortingDetailApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetStoreSortingDetailApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetStoreSortingDetailApiLogic {
	hostname, _ := os.Hostname()
	return &GetStoreSortingDetailApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetStoreSortingDetailApiLogic) GetStoreSortingDetailApi(req *types.IdReq) (resp *types.GetStoreSortingDetailResp, err error) {
	// 获取基本信息
	sortingPageBoResp, err := service.StoreSortingService.GetStoreSortingBaseApi(l.ctx, req.Id)
	if err != nil {
		return nil, err
	}
	// 获取详情数据
	entities, err := service.StoreSortingDetailService.GetStoreSortingDetailApi(l.ctx, req.Id)
	if err != nil {
		return nil, err
	}
	var dataList = make([]types.StoreSortingDetailModel, 0)
	for _, item := range entities {
		dataList = append(dataList, types.StoreSortingDetailModel{
			ID:                      item.ID,                                 // 主键id
			StoreSortingID:          item.StoreSortingID,                     // 分拣单store_sorting主键id
			SpuID:                   item.SpuID,                              // 商品,关联到spu表主键id
			SnapshotSpuName:         item.SnapshotSpuName,                    // 快照商品名称
			SnapshotProductImageURL: item.SnapshotProductImageURL,            // 快照产品图片
			SnapshotSpuType:         item.SnapshotSpuType,                    // 快照商品类型,0表示基础商品,1表示加工商品
			SnapshotUnit:            item.SnapshotUnit,                       // 快照单位
			SnapshotCategoryID:      item.SnapshotCategoryID,                 // 快照分类id
			SnapshotCategory:        item.SnapshotCategory,                   // 快照分类
			LossRate:                item.LossRate.StringFixed(2),            // 快照损耗率
			OrdersCount:             item.OrdersCount.StringFixed(2),         // 下单数量
			OutboundCount:           item.OutboundCount.StringFixed(2),       // 出库数量
			OperateName:             item.OperateName,                        // 出库员
			OperateID:               item.OperateID,                          // 出库员id,关联到员工表
			Remark:                  item.Remark,                             // 备注
			CreatedAt:               utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt:               utils.TimestampToString(item.UpdatedAt), // 更新时间
		})
	}
	return &types.GetStoreSortingDetailResp{
		BaseInfo: types.StoreSortingModel{
			ID:            sortingPageBoResp.ID,                                 // 主键id
			OrderNo:       sortingPageBoResp.OrderNo,                            // 订单流水号
			CustomerID:    sortingPageBoResp.CustomerID,                         // 客户id,关联到enterprise_customer表主键id
			CustomerName:  sortingPageBoResp.CustomerName,                       // 客户id,关联到enterprise_customer表主键id
			TotalAmount:   sortingPageBoResp.TotalAmount.StringFixed(2),         // 订单金额
			TotalCount:    sortingPageBoResp.TotalCount.StringFixed(2),          // 订单商品数量
			WarehouseID:   sortingPageBoResp.WarehouseID,                        // 出库仓库,关联到warehouse表主键id
			WarehouseName: sortingPageBoResp.WarehouseName,                      // 出库仓库,关联到warehouse表主键id
			OrdersID:      sortingPageBoResp.OrdersID,                           // 关联到订单表id,orders表主键id
			OrdersOrderNo: sortingPageBoResp.OrdersOrderNo,                      // 关联到订单表id,orders表主键id
			OperateName:   sortingPageBoResp.OperateName,                        // 制单员
			OperateID:     sortingPageBoResp.OperateID,                          // 制单员id,关联到员工表
			Remark:        sortingPageBoResp.Remark,                             // 备注
			CreatedAt:     utils.TimestampToString(sortingPageBoResp.CreatedAt), // 创建时间
			UpdatedAt:     utils.TimestampToString(sortingPageBoResp.UpdatedAt), // 更新时间
		},
		DataList: dataList,
	}, nil
}
